<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

$error = FALSE;

if($_POST) {
    switch($_POST['form']) {
        case 'account':
            $ret = $userMgr->update($_SESSION['user']['id'], $_POST);
            if($ret === FALSE) {
                $_SESSION['user']['username'] = $_POST['username'];
                $_SESSION['user']['name'] = $_POST['name'];
                $_SESSION['user']['language_id'] = $_POST['language_id'];
                $_SESSION['msg'] = $msgMgr->get('account-update-success');
                header('Location: /viewuser.php?id=' . $_SESSION['user']['id']);
                exit();
            } else {
                $error = $ret;
            }
            break;
        case 'delete':
            $userMgr->delete($_SESSION['user']['id']);
            $userMgr->logout();
            header('Location: /login.php?closed');
            exit();
            break;
    }
}

$languages = $langMgr->getLanguages();
$language_options = array();
foreach($languages as $row) {
    $language_options[$row['id']] = '<option value="'. $row['id'] . '" ' . ($row['id'] == $_SESSION['user']['language_id'] ? 'selected' : '') . '>' . $row['name'] . '</option>';
}

$replaces = array(
    'username'      => $_SESSION['user']['username'],
    'name'          => $_SESSION['user']['name'],
    'language_options' => implode("\n", $language_options),
    'error'         => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_editmyaccount'  => $langMgr->get('header_editmyaccount'),
    'header_findfriends'    => $langMgr->get('header_findfriends'),
    'header_closeaccount'   => $langMgr->get('header_closeaccount'),
    'text_email'            => $langMgr->get('text_email'),
    'text_name'             => $langMgr->get('text_name'),
    'text_language'         => $langMgr->get('text_language'),
    'text_changepassword'   => $langMgr->get('text_changepassword'),
    'text_confirmpassword'  => $langMgr->get('text_confirmpassword'),
    'text_searchintro'      => $langMgr->get('text_searchintro'),
    'text_closeaccount'     => $langMgr->get('text_closeaccount'),
    'btn_save'              => $langMgr->get('btn_save'),
    'btn_search'            => $langMgr->get('btn_search'),
    'btn_confirm'           => $langMgr->get('btn_confirm'),
    'text_gravatar'         => $langMgr->get('text_gravatar'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
